<?php
require_once("treeBuilder.php");
$pid = $_REQUEST["pid"];
$target = $_REQUEST["target"];
$builder = new treeBuilder($pid, $target);
$all_routes = $builder->getRoutes();
$direct = count($all_routes["direct"]) > 0;
$routes = $direct ? $all_routes["direct"] : $all_routes["suggest"];
ob_start();
echo $direct ? "<p><b>Сведения о маршрутах:</b></p>" : "<p><b>Прямой маршрут не найден.</b></p><p>Маршрут до ближайшей точки:</p>";
foreach ($routes as $i => $route) {
    $id = "route-info-$i";
    ?>
    <div class="<?= $i > 2 ? "main-route additional" : "main-route" ?>">
    <input type="radio" name="route" id="<?= $id ?>" value="<?= $i ?>"
           <? if ($i == 0) : ?>checked="checked"<? endif; ?> /><label for="<?= $id ?>">Маршрут <?= $i + 1 ?></label>
    <br/>
    Длина: <?= number_format($route["length"] * 1000, 2) ?> м<br/>
    Затухание: <?= number_format($route["attenuation"], 3) ?> дб<br/>
    <a class="route-details" data-id="<?=$i ?>" href="javascript:void(0)">Подробнее</a>
	</div>
<?
}
if (count($routes) > 3) : ?>
<br/><a class="route-toggle-all" href="javascript:void(0)">Показать все (<?=count($routes)?>)</a>
<? endif; 
$markup = ob_get_clean();
echo json_encode(array("routes" => $routes, "markup" => $markup));
?>
